﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.Models;

namespace HRM.Controllers
{ 
    public class EmployeeController : Controller
    {
        private HRMDBContext db = new HRMDBContext();

        //
        // GET: /Employee/

        public ViewResult Index()
        {
            var employees = db.Employees.Include(e => e.Catagory).Include(e => e.Project).Include(e => e.Designation);
            return View(employees.ToList());
        }
        [HttpPost]
        public ViewResult Index(int empId)
        {
            if (empId>0)
            {
                try
                {
                    List<Employee> employees = new List<Employee>();
                    // EmployeeManager manager=new EmployeeManager();
                    //  employees = manager.SearchEmployeeById(empId);
                    var employee = db.Employees.Find(empId);
                    var designation = db.Designations.Find(employee.DesignationID);
                    var catagory = db.Catagories.Find(employee.CatagoryID);
                    var project = db.Projects.Find(employee.ProjectID);
                    employee.Designation = designation;
                    employee.Catagory = catagory;
                    employee.Project = project;
                    employees.Add(employee);
                    return View(employees);
                }
                catch (DataException)
                {
                    ModelState.AddModelError("Unable to read Employee Information", "");
                }
              //  return View("Error"); 
            }
            return View("Error"); 
        }

        //
        // GET: /Employee/Details/5

        public ViewResult Details(int id)
        {
            Employee employee = db.Employees.Find(id);
            return View(employee);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            ViewBag.CatagoryID = new SelectList(db.Catagories, "CatagoryID", "EmpCatagory");
            ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "ProjectName");
            ViewBag.DesignationID = new SelectList(db.Designations, "Id", "Title");
            return View();
        } 

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.CatagoryID = new SelectList(db.Catagories, "CatagoryID", "EmpCatagory", employee.CatagoryID);
            ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "ProjectName", employee.ProjectID);
            ViewBag.DesignationID = new SelectList(db.Designations, "Id", "Title", employee.DesignationID);
            return View(employee);
        }
        
        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            Employee employee = db.Employees.Find(id);
            ViewBag.CatagoryID = new SelectList(db.Catagories, "CatagoryID", "EmpCatagory", employee.CatagoryID);
            ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "ProjectName", employee.ProjectID);
            ViewBag.DesignationID = new SelectList(db.Designations, "Id", "Title", employee.DesignationID);
            return View(employee);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Entry(employee).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CatagoryID = new SelectList(db.Catagories, "CatagoryID", "EmpCatagory", employee.CatagoryID);
            ViewBag.ProjectID = new SelectList(db.Projects, "ProjectID", "ProjectName", employee.ProjectID);
            ViewBag.DesignationID = new SelectList(db.Designations, "Id", "Title", employee.DesignationID);
            return View(employee);
        }

        //
        // GET: /Employee/Delete/5
 
        public ActionResult Delete(int id)
        {
            Employee employee = db.Employees.Find(id);
            return View(employee);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Employee employee = db.Employees.Find(id);
            db.Employees.Remove(employee);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
   
}